package cn.huaqingcheng.tianshu.application.http;

import cn.huaqingcheng.tianshu.core.auth.param.SimpleLoginParam;
import cn.huaqingcheng.tianshu.core.auth.session.SessionService;
import cn.huaqingcheng.tianshu.core.auth.view.LoginResponse;
import cn.huaqingcheng.tianshu.core.uc.dto.AccountParam;
import cn.huaqingcheng.tianshu.core.uc.model.Account;
import cn.huaqingcheng.tianshu.core.uc.service.AccountService;
import cn.huaqingcheng.tianshu.security.annotation.CurrentAccountId;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import jakarta.annotation.security.PermitAll;
import jakarta.validation.Valid;

/**
 * 登录和会话
 */
@RestController
@RequiredArgsConstructor
public class UserAccountController {

    private final SessionService service;

    private final AccountService accountService;


    /**
     * 登录
     */
    @PermitAll
    @PostMapping("/token")
    public LoginResponse token(@RequestBody @Valid SimpleLoginParam param) {
        return service.login(param);
    }

    /**
     * 更新账号信息
     */
    @PutMapping("/account")
    public Account updateAccount(
            @CurrentAccountId Long accountId,
            @RequestBody AccountParam account
    ) {
        return accountService.updateAccount(accountId, account);
    }

}
